/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.ui;

import junit.framework.TestCase;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Shell;
import org.leeloo.core.Tag;
import org.leeloo.core.TagService;

public class TagFilterManagerTest extends TestCase {
    private Shell shell;
    private TableViewer viewer;
    private TagService tagService;
    private TagFilterManager manager;
    private Tag tag;
    
    protected void setUp() throws Exception {
        shell = new Shell();
        viewer = new TableViewer(shell);
        tagService = new TagService();
        manager = new TagFilterManager(viewer, tagService);
        tag = new Tag("", "");
    }
    
    public void testAddTag() throws Exception {
        assertEquals(0, manager.getTags().length);        
        manager.addTag(tag);
        assertEquals(1, manager.getTags().length);
        assertEquals(tag, manager.getTags()[0]);
    }
    
    public void testAddDuplicateTag() throws Exception {
        assertEquals(0, manager.getTags().length);
        manager.addTag(tag);
        manager.addTag(tag);
        assertEquals(1, manager.getTags().length);
    }
    
    public void testSetSelection() throws Exception {
        assertEquals(0, manager.getSelection().length);
        assertEquals(0, viewer.getFilters().length);
        
        manager.addTag(tag);
        manager.setSelection(new Tag[] {tag});
        
        Tag[] selection = manager.getSelection();
        assertEquals(1, selection.length);        
        assertEquals(tag, selection[0]);
        assertEquals(1, viewer.getFilters().length);
    }
    
    public void testSetDuplicateSelection() throws Exception {
        assertEquals(0, manager.getSelection().length);
        assertEquals(0, viewer.getFilters().length);
        
        manager.addTag(tag);
        manager.setSelection(new Tag[] {tag, tag});
        
        Tag[] selection = manager.getSelection();
        assertEquals(1, selection.length);        
        assertEquals(tag, selection[0]);
        assertEquals(1, viewer.getFilters().length);
    }
    
    public void testSetIllegalSelection() throws Exception {
        try {
            manager.setSelection(new Tag[] {tag});
            fail("exception should have been thrown");
        } catch (IllegalArgumentException e) {
        }        
    }
    
    public void testClear() throws Exception {
        manager.addTag(tag);
        manager.setSelection(new Tag[] {tag});
        
        assertEquals(1, manager.getTags().length);
        assertEquals(1, manager.getSelection().length);
        assertEquals(1, viewer.getFilters().length);
        
        manager.clear();
        
        assertEquals(0, manager.getTags().length);
        assertEquals(0, manager.getSelection().length);
        assertEquals(0, viewer.getFilters().length);
    }
}